package lvcy.message.sender.builder.impl;

import java.lang.reflect.Constructor;
import java.util.List;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import lvcy.message.entity.RequestEntity;
import lvcy.message.sender.DxtMessageSender;
import lvcy.message.sender.MessageSender;
import lvcy.message.sender.builder.DxtMessageSenderBuilder;

public class DxtMultiMessageSenderBuilder extends DxtMessageSenderBuilder{

	private Logger log=LogManager.getLogger(this);
	@Override
	public MessageSender build(RequestEntity request) {
		String postData=getPostData(request);
		try {
			Class<?> clazz=Class.forName(DxtMessageSender.class.getName());
			Constructor<?> constructor=clazz.getDeclaredConstructor(String.class,String.class);
			sender=(DxtMessageSender)constructor.newInstance("",postData);
		} catch (Exception e) {
			log.error(e);
		}
		return sender;
	}

	protected String getPostData(RequestEntity request){
		StringBuilder builder=new StringBuilder(super.getPostData(request));
		builder.append("Phones=").append(getPhones(request.getPhones()));
		return builder.toString();
	}
	private String getPhones(List<String> phones){
		StringBuilder builder=new StringBuilder();
		for(String phone:phones){
			builder.append(phone).append(",");
		}
		return builder.toString();
	}

}
